using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.Network;
using OracleInternal.ServiceObjects;

namespace OracleInternal.TTC
{
	internal class TTCDirectPathMOP : TTCFunction
	{
		private const short ODPMOP = 130;

		public const int DPMOPOPC_ABORT = 1;

		public const int DPMOPOPC_FINISH = 2;

		public const int DPMOPOPC_FLUSH = 3;

		public const int DPMOPOPC_DATASAVE = 4;

		public const int DPMOPOPC_SETIU = 5;

		public const int DPMOPOPC_FINISH_ONLY = 6;

		public const int DPMOPOPC_ABORT_ONLY = 7;

		public const int DPMOPOPC_CALL_KDBLAI = 8;

		public const int DPMOPDEF_DATASAVEOPT_SAVEONLY = 0;

		public const int DPMOPDEF_DATASAVEOPT_FINISH = 1;

		public const int DPMOPDEF_DATASAVEOPT_PARTIAL = 2;

		public const int DPMOPDEF_IN_PARMCOUNT = 1;

		public const int DPMOPDEF_OUT_PARMCOUNT = 0;

		private DirectPathContext m_directPathContext;

		private long[] m_dpmopi4;

		private int m_dpmopi4l;

		private long[] m_dpmopo4;

		internal TTCDirectPathMOP(MarshallingEngine mEngine)
			: base(mEngine, 130, 0)
		{
			m_dpmopi4 = new long[1];
			m_dpmopi4l = 0;
		}

		internal void Init(DirectPathContext directPathCtx)
		{
			m_directPathContext = directPathCtx;
			m_dpmopo4 = null;
		}

		internal void Commit()
		{
			WriteMessage(2);
			ReadResponse();
		}

		internal void Abort()
		{
			WriteMessage(1);
			ReadResponse();
		}

		internal void WriteMessage(int OpCode)
		{
			WriteFunctionHeader();
			m_marshallingEngine.MarshalUB4(OpCode);
			m_marshallingEngine.MarshalSWORD((int)m_directPathContext.m_tableCursor);
			if (m_dpmopi4l > 0)
			{
				m_marshallingEngine.MarshalPointer();
			}
			else
			{
				m_marshallingEngine.MarshalNullPointer();
			}
			m_marshallingEngine.MarshalUWORD(m_dpmopi4l);
			m_marshallingEngine.MarshalPointer();
			m_marshallingEngine.MarshalPointer();
			for (int i = 0; i < m_dpmopi4l; i++)
			{
				m_marshallingEngine.MarshalUB4(m_dpmopi4[i]);
			}
		}

		internal void ReadResponse()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCTransactionEN, OracleTraceFuncName.ReadResponse);
			}
			bool flag = false;
			byte b = 0;
			try
			{
				m_marshallingEngine.TTCErrorObject.Initialize();
				while (!flag)
				{
					try
					{
						switch (m_marshallingEngine.UnmarshalSB1())
						{
						case 23:
							ProcessServerSidePiggybackFunction(0);
							break;
						case 8:
							Process_RPA_Message();
							break;
						case 15:
							m_marshallingEngine.TTCErrorObject.ReadWarning();
							break;
						case 4:
							flag = true;
							m_marshallingEngine.TTCErrorObject.ReadErrorMessage();
							if (m_marshallingEngine.TTCErrorObject.ErrorCode == 1403)
							{
								m_marshallingEngine.TTCErrorObject.Initialize();
							}
							else
							{
								OracleConnectionImpl.CheckForAnyErrorFromDB(m_marshallingEngine.TTCErrorObject);
							}
							break;
						case 9:
							if (m_marshallingEngine.HasEOCSCapability)
							{
								m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
							}
							flag = true;
							break;
						default:
							throw new Exception("TTC error");
						}
					}
					catch (NetworkException ex)
					{
						if (ex.ErrorCode != 3111)
						{
							throw;
						}
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
					catch (Exception)
					{
						if (m_marshallingEngine.m_oraBufRdr != null)
						{
							m_marshallingEngine.m_oraBufRdr.ClearState();
						}
						if (!flag)
						{
							m_marshallingEngine.m_oracleCommunication.Break();
							m_marshallingEngine.m_oracleCommunication.Reset();
							continue;
						}
						throw;
					}
				}
			}
			catch (Exception ex3)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCTransactionEN, OracleTraceFuncName.ReadResponse, ex3);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCTransactionEN, OracleTraceFuncName.ReadResponse);
				}
			}
		}

		private void Process_RPA_Message()
		{
			int num = m_marshallingEngine.UnmarshalUB2();
			if (num > 0)
			{
				m_dpmopo4 = new long[num];
				for (int i = 0; i < num; i++)
				{
					m_dpmopo4[i] = m_marshallingEngine.UnmarshalUB4();
				}
			}
			else
			{
				m_dpmopo4 = null;
			}
		}
	}
}
